package copyfile01;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
利用FileReader和FileWriter拷贝文件
字符流拷贝只能拷贝文本类文件，不能拷贝纯字节的文件(图片，音频)
 */
public class CoppyFileDemo01 {
    public static void main(String[] args) throws IOException {
        //method01();
        //method02();
        FileReader fr = new FileReader("Day24/风景.jpg");
        FileWriter fw = new FileWriter("Day24/风景_copy.jpg");
        int length;
        char[] chars = new char[1024];
        while ((length = fr.read(chars)) != -1) {
            fw.write(chars);
        }
    }

    private static void method02() throws IOException {
        //1.利用FileReader和FileWriter关联源文件和目的文件
        FileReader fr = new FileReader("Day24/src/Day24笔记.txt");
        FileWriter fw = new FileWriter("Day24/Day24笔记_copy_2.txt");

        //2.利用read()和write()方法进行读取
        //将内同读到一个字符数组中去，将这两个字符数组写到文件中
        int length;
        char[] chars = new char[1024];//一般声明一个1024的整数倍
        while ((length = fr.read(chars)) != -1) {
            fw.write(chars,0,length);
        }
        fr.close();
        fw.close();
    }

    private static void method01() throws IOException {
        //1.利用FileReader和FileWriter关联源文件和目的文件
        FileReader fr = new FileReader("Day24/src/Day24笔记.txt");
        FileWriter fw = new FileWriter("Day24/Day24笔记_copy.txt");

        //2.利用read()和write()方法进行读取
        int ch;
        while ((ch = fr.read()) != -1) {
            fw.write(ch);
        }
        fr.close();
        fw.close();
    }
}
